<?php
require_once "db.php";

class Usuarios extends Db {
	public $id_usuario;
	public $nome;
	public $email;
	public $username;
	public $password;

	
	public function newUser(){
		try {
			$this->getConnect();
			mysql_query("INSERT INTO usuarios VALUES(".$this->nome.",".$this->email.",".$this->username.",".$this->password.")", $this->conn);
			$this->disconnect();
		} catch(Exception $e) {}
	}

	public function updateUser(){
		try {
			$this->getConnect();
			mysql_query("UPDATE usuarios SET nome='".$this->nome."', email='".$this->email."', username='".$this->username."', password='".$this->password."'", $this->conn);
			$this->disconnect();
		} catch (Exception $e) {
		}
	}

	public function deleteUser($id){
		try {
			$this->getConnect();
			$result = mysql_query("SELECT * FROM usuarios WHERE id_usuario='".$id."'", $this->conn);
			$row = mysql_fetch_array($result);
			if(count($row) > 1) {
				mysql_query("DELETE FROM usuarios WHERE id_usuario='".$id."'", $this->conn);
				echo "Usuário deletado com sucesso!";
			} else {
				echo "Usuário não encontrado.";
			}
			$this->disconnect();
		} catch (Exception $e) {
		}
	}

	public function findUserById($id){
		try {
			$this->getConnect();
			$result = mysql_query("SELECT * FROM usuarios WHERE id_usuario='".$id."'", $this->conn);
			$rows = mysql_fetch_array($result);
			if(count($rows) >= 1) {
				while($rows) {
					return $array = array("id_usuario" => $rows["id_usuario"], "nome" => $rows["nome"], "email" => $rows["email"], "username" => $rows["username"], "password" => $rows["password"]);
				}
			}
			$this->disconnect();
		} catch (Exception $e) {
		}
	}

	public function findAll(){
		try {
			$this->getConnect();
			$result = mysql_query("SELECT * FROM usuarios", $this->conn);
			$rows = mysql_fetch_array($result);
			if(count($rows) >= 1) {
				$i = 0;
				while($rows) {
					$array[$i] = array("id_usuario" => $rows["id_usuario"], "nome" => $rows["nome"], "email" => $rows["email"], "username" => $rows["username"], "password" => $rows["password"]);
					$i++;
				}
				return $array;
			}
			$this->disconnect();
		} catch (Exception $e) {
		}
	}

	public function getLogin(){
		$this->getConnect();
			$result = mysql_query("SELECT * FROM usuarios WHERE username='".$this->username."' AND password ='".$this->password."'", $this->conn);
			$row = mysql_fetch_array($result);
			if(count($row) > 1) {
				return true;
			} else {
				return false;
			}
	}
	
	public function getLogout(){
		
	}
}